Sprite Byte™: Scaling

Level: Beginner - Intermediate

by Alyce Watson [http://alycesrestaurant.com/]

Home

Newsletter Index - Terra

Sprite Byte - Watson

Filtered Numeric Input - Bromley

Changing the cursor - Sweet

QBASIC to Liberty BASIC - Sweet

Contact Manager - Lewis

LameCard - Nally

Submission Guildlines

Newsletter Help

Index


Changing the Sizes of Sprites at Runtime

You can change the size of any sprite at runtime. This is very easy with the "spritescale" command. The format of the command is:

    #w.g "spritescale spritename percent"

The actual code looks like this, where "smiley" is the name of the sprite and "75" is the percent to scale:

    #w.g "spritescale smiley 75"
    #w.g "drawsprites"

What Does the Percent Mean?

The value for percent refers to the change in both the width and the height. If a sprite is scaled to 50 percent, it is 50 percent as wide and 50 percent as high. That does not mean that it is half as big as normal. It means that it is only 25% as big as normal! This is because both the width and height were reduced by 50 percent. Consider a sprite that is 100x100 pixels. It is 1000 pixels in area (100*100). If both width and height are reduced to 50 pixels, it is then 250 pixels in area (50*50). Here are two images. The first is a sprite at normal size, the second is scaled with a percent value of 50.

SCALE 100: SCALE 50:

How They Look!

Here are some possible scale values:

SCALE 100: SCALE 50: SCALE 200: SCALE 75:

What Can You Do With Scaled Sprites?

One sprite can have different appearances as needed in the program. Perhaps a sprite needs to grow larger over time. It might be necessary to change the size of a sprite when it becomes more powerful or less powerful in a game. Perhaps this method is most useful when a sprite appears in three-dimensional space. As it moves away from the camera (usually towards the top of the screen) it gets smaller. As it moves towards the camera, it gets larger.


DEMOS

Here are two spritescale demos.


In the following demo, change this line:

    #w.g "spritescale smiley 75"

Replace the number "75" with the desired scale value to see how it affects the appearance of the sprite.

'run from LB root directory
nomainwin
WindowWidth=150:WindowHeight=150
loadbmp "smiley1", "sprites\smiley1.bmp"
graphicbox #w.g, 0, 0, 400, 300
open "sprite test" for window_nf as #w
    #w "trapclose [quit]"
    #w.g "down"
    #w.g "addsprite smiley smiley1"
    #w.g "spritescale smiley 75"
    #w.g "drawsprites"

    wait

[quit]
unloadbmp "smiley1"
close #w:end

This demo shows a sprite that starts out very small and far away and moves closer. As it appears to move closer, it gets larger.

'run from LB root directory
nomainwin
WindowWidth=150:WindowHeight=350
loadbmp "smiley1", "sprites\smiley1.bmp"
graphicbox #w.g, 0, 0, 400, 300
open "sprite test" for window_nf as #w
    #w "trapclose [quit]"
    #w.g "down"
    #w.g "addsprite smiley smiley1"
    #w.g "drawsprites"

for i = 1 to 200
    #w.g "spritexy smiley 0 ";i
    #w.g "spritescale smiley ";i
    #w.g "drawsprites"
    calldll #kernel32, "Sleep",20 as long, re as void
    scan
next

    wait

[quit]
unloadbmp "smiley1"
close #w:end


Home

Newsletter Index - Terra

Sprite Byte - Watson

Filtered Numeric Input - Bromley

Changing the cursor - Sweet

QBASIC to Liberty BASIC - Sweet

Contact Manager - Lewis

LameCard - Nally

Submission Guildlines

Newsletter Help

Index